<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button
          type="primary"
          @click="handleForm({})"
          v-auth="'bas:mes:level:basOperatorLevel:edit'"
        >
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
        <a-button type="default" @click="handleExport()">
          <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <a-button type="default" @click="handleImport()">
          <Icon icon="ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.code }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsBasMesLevelBasOperatorLevelList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    basOperatorLevelDelete,
    basOperatorLevelListData,
    basOperatorLevelDisable,
    basOperatorLevelEnable,
  } from '/@/api/bas/mes/level/basOperatorLevel';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl2 } from '/@/utils/file/download';
  import { useModal } from '/@/components/Modal';

  const { t } = useI18n('bas.mes.level.basOperatorLevel');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('操作工技能等级管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 100,
    schemas: [
      {
        label: t('操作工编码'),
        field: 'code',
        component: 'Input',
      },
      {
        label: t('操作工名称'),
        field: 'basOperator.name',
        component: 'Input',
      },
      {
        label: t('存货编码'),
        field: 'invCode',
        component: 'Input',
      },
      {
        label: t('存货名称'),
        field: 'basInv.cinvname',
        component: 'Input',
      },
      {
        label: t('工序编码'),
        field: 'operCode',
        component: 'Input',
      },
      {
        label: t('工序名称'),
        field: 'basOper.name',
        component: 'Input',
      },
      {
        label: t('当前等级'),
        field: 'techLevel',
        component: 'Select',
        componentProps: {
          dictType: 'mes_tech_level',
          allowClear: true,
        },
      },
      {
        label: t('等级调整日期'),
        field: 'justDate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD',
          showTime: false,
        },
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'sys_search_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('操作工编码'),
      dataIndex: 'code',
      key: 'a.code',
      sorter: true,
      width: 130,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('操作工名称'),
      dataIndex: 'basOperator.name',
      key: 'operator.name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('存货编码'),
      dataIndex: 'invCode',
      key: 'a.inv_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('存货名称'),
      dataIndex: 'basInv.cinvname',
      key: 'inv.cinvname',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('工序编码'),
      dataIndex: 'operCode',
      key: 'a.oper_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('工序名称'),
      dataIndex: 'basOper.name',
      key: 'oper.name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('当前等级'),
      dataIndex: 'techLevel',
      key: 'a.tech_level',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'mes_tech_level',
    },
    {
      title: t('等级调整日期'),
      dataIndex: 'justDate',
      key: 'a.just_date',
      sorter: true,
      width: 120,
      align: 'center',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 80,
      align: 'center',
      dictType: 'sys_search_status',
    },
    {
      title: t('更新日期'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('备注说明'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑操作工技能等级'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'bas:mes:level:basOperatorLevel:edit',
      },
      {
        icon: 'ant-design:stop-outlined',
        color: 'error',
        title: t('停用操作工技能等级'),
        popConfirm: {
          title: t('是否确认停用操作工技能等级'),
          confirm: handleDisable.bind(this, { id: record.id }),
        },
        auth: 'bas:mes:level:basOperatorLevel:edit',
        ifShow: () => record.status === '0',
      },
      {
        icon: 'ant-design:check-circle-outlined',
        color: 'success',
        title: t('启用操作工技能等级'),
        popConfirm: {
          title: t('是否确认启用操作工技能等级'),
          confirm: handleEnable.bind(this, { id: record.id }),
        },
        auth: 'bas:mes:level:basOperatorLevel:edit',
        ifShow: () => record.status === '2',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除操作工技能等级'),
        popConfirm: {
          title: t('是否确认删除操作工技能等级'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'bas:mes:level:basOperatorLevel:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: basOperatorLevelListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl2({
      url: ctxAdminPath + '/bas/mes/level/basOperatorLevel/exportData',
      params: getForm().getFieldsValue(),
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  async function handleDisable(record: Recordable) {
    const res = await basOperatorLevelDisable(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handleEnable(record: Recordable) {
    const res = await basOperatorLevelEnable(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handleDelete(record: Recordable) {
    const res = await basOperatorLevelDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
